/**面板和提示语句显示, 面板适配，弹窗动画等相关函数 */
var GPanelTips = {
    
    /**显示提示，info必须， showTime不必须，默认2秒*/
    showTips(info, showTime){
        showTime = showTime || 2;
        //通用路径
        var url = "panel/tips";
        cc.loader.loadRes(url, (err, prefab) => {
            if(err){
                console.error(err.message || err);
                return;
            }
            var tip = cc.instantiate(prefab);
            tip.parent = cc.director.getScene();
            tip.position = cc.v2(cc.winSize.width/2, cc.winSize.height/2);
            tip.zIndex = 9999;//提示层级最高
            tip.getChildByName("info").getComponent(cc.Label).string = info.toString();
         
            tip.setScale(5);
            var show = cc.scaleTo(0.2, 1);
            var fadeOut = cc.fadeOut(showTime);
            var callFunc = cc.callFunc(function(){
                tip.destroy();
            });
            tip.runAction(cc.sequence(show, fadeOut, callFunc));
        });
    },

    /**提示金币（hp，钻石等）的减少（增加）动画 减少为红色，增加为绿色   
     * change 为变化值，可正可负，正为绿色，负为红色 必须  
     * parent 父节点, 必须  
     * startPosition 为提示数字出现的位置 必须  
     * 动画为渐隐，上移
    */
    showNumChange(change, parent, startPosition){
        //通用路径
        var url = "panel/num";
        cc.loader.loadRes(url, (err, prefab) => {
            if(err){
                console.error(err.message || err);
                return;
            }
            var num = cc.instantiate(prefab);
            if(change > 0){
                change = "+" + change;
            }
            num.getComponent(cc.Label).string = change;
            if(change > 0){
                num.color = cc.Color.YELLOW;
            }else{
                num.color = cc.Color.RED;
            }
            num.parent = parent;
            num.position = startPosition;

            var moveBy = cc.moveBy(1, 0, 50);
            var fadeOut = cc.fadeOut(1);
            var callFunc = cc.callFunc(()=>{
                num.destroy();
            });
            num.runAction(cc.sequence(cc.spawn(moveBy, fadeOut), callFunc));
        });
    },
   
    /**显示弹窗面板*/
    showPanel(url){
        cc.loader.loadRes(url, (err, prefab) => {
            if(err){
                console.error(err.message || err);
                return;
            }
            var panel = cc.instantiate(prefab);
            panel.parent = cc.director.getScene();
            panel.position = cc.v2(cc.winSize.width/2, cc.winSize.height/2);
        });
    },

    /**显示弹窗面板并初始化数据, 3参数必须 */
    showPanelWithInit(url, scriptName, initData, parent, zIndex){
        parent = parent || cc.director.getScene();
        zIndex = zIndex || 0;
        cc.loader.loadRes(url, (err, prefab) => {
            if(err){
                console.error(err.message || err);
                return;
            }
            var panel = cc.instantiate(prefab);
            panel.getComponent(scriptName).init(initData);
            panel.parent = parent;
            panel.zIndex = zIndex;
            panel.position = cc.v2(cc.winSize.width/2, cc.winSize.height/2);
        });
    },

    /**底部ui适配, node为要适配的节点，上面必须挂载Widget组件，offset可选，距离底部的偏移量，默认0  
     * 返回Y方向上的偏移量
    */
    adaptation(node, offset){
        if (cc.sys.platform === cc.sys.WECHAT_GAME){
            //记录初始Y坐标
            var initY = node.y;
            if(offset == undefined){
                offset = 0;
            }
            var bottom = 0;
            //x额外提升
            var extra = 0;
            var getSystemInfoSync = wx.getSystemInfoSync();
            var windowWidth = getSystemInfoSync.windowWidth;
            //缩放比例
            var rate = 750 / windowWidth;
            if(getSystemInfoSync.model.indexOf("iPhone X") > -1 || getSystemInfoSync.model.indexOf("unknown") > -1 || getSystemInfoSync.model.indexOf("iPhone XS") > -1){
                console.warn("iPhone X系列");
                extra = 20;
            }else{
                extra = 0;
            }
            bottom = (windowWidth * 0.35 + extra) * rate + offset;
            node.getComponent(cc.Widget).bottom = bottom;
            node.getComponent(cc.Widget).updateAlignment();
            var endY = node.y;
            return endY - initY;
        }
    },

    /**面板弹窗动画 + ui适配banner  
     * total 节点既为弹窗缩放动画的父节点，也为ui适配banner的父节点    
     * childName 要适配的子节点名称，默认为total  
     * offset可选，距离底部的偏移量，默认0
    */
    panelAnimAndAdaptation(node, childName, offset){
        childName = childName || "total";
        var total = node.getChildByName(childName);
        if(!total){
            console.error("找不到total节点！！!");
            return;
        }
        total.opacity = 0;
        //ui适配
        this.adaptation(total, offset);
        //弹窗动画
        var delayTime = cc.delayTime(0.1);
        var callFunc = cc.callFunc(function(target, offset) {
            total.setScale(0.1);
            total.opacity = 255;
        });
        var scale = cc.scaleTo(0.5, 1, 1).easing(cc.easeBackOut());
        total.runAction(cc.sequence(delayTime, callFunc, scale));
    },

    /**某个节点的弹窗动画  
     * node: 要显示弹窗动画的节点  
     * time: 弹窗动画持续时间，默认0.3秒，可选
     */
    popUpAnimation(node, time){
        time = time || 0.3;
        node.opacity = 0;
        var delayTime = cc.delayTime(0.1);
        var callFunc = cc.callFunc(()=> {
            node.setScale(0.1);
            node.opacity = 255;
        });
        var scale = cc.scaleTo(time, 1, 1).easing(cc.easeBackOut());
        node.runAction(cc.sequence(delayTime, callFunc, scale));
    },

    /**创建获取玩家信息的授权按钮 */
    createUserInfoButton(callBack){
        if(window.wx){
            var canvas = cc.game.canvas;
            var button = wx.createUserInfoButton({
                withCredentials: false,
                type: 'image',
                image: "",
                style: {
                    left: 0,
                    top: 0,
                    width: canvas.width,
                    height: canvas.height,
                },
                lang:"zh_CN"
            });
            button.show();
            button.onTap((res)=>{
                //console.warn("获取用户信息", res);
                if(callBack){
                    callBack(res);
                }
                button.destroy();
            })
        }
    },
};

window.GPanelTips = GPanelTips;